package com.bw.zg3.notice.Car1

import android.view.View
import android.view.animation.AnimationUtils
import android.view.animation.BounceInterpolator
import android.widget.PopupMenu
import android.widget.SearchView.OnQueryTextListener
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3.R
import com.bw.zg3.databinding.ActivityCar2Binding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
@AndroidEntryPoint
@Route(path = "/app/Car1Activity")
class Car1Activity : BaseActivity<ActivityCar2Binding,H1ViewModel>() {

        val car1Apter by lazy { H1Apter() }
    override fun initView() {
        binding.carrv.apply {
            adapter = car1Apter
        }
        lifecycleScope.launch {
            vm.H1REsult(1, "区域").collect {
                car1Apter.submitData(it)
            }
        }
        //popupmenu的写法
//        binding.fl.setOnClickListener {
//            val popupMenu = PopupMenu(this, binding.fl)
//            popupMenu.menu.add("空闲")
//            popupMenu.menu.add("已占用")
//            popupMenu.menu.add("维修中")
//
//            var para = ""
//            popupMenu.setOnMenuItemClickListener {
//                when (it.title) {
//                    "空闲" -> {
//                        para = "空闲"
//                    }
//                    "已占用" -> {
//                        para = "已占用"
//                    }
//                    "维修中" -> {
//                        para = "维修中"
//                    }
//                }
//
//                // 使用 lifecycleScope.launch 启动协程，执行后台任务
//                lifecycleScope.launch {
//                    vm.H1REsult(para, "状态").collect {
//                        // 假设 car1Apter 是 RecyclerView 的适配器，将数据提交给适配器
//                        car1Apter.submitData(it)
//                    }
//                }
//
//                // 返回 true 表示事件已处理
//                return@setOnMenuItemClickListener true
//            }
//
//            // 显示 popupMenu
//            popupMenu.show()
//        }
        binding.fl.setOnClickListener {
            binding.cl.visibility=View.VISIBLE
            val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.`in`)
            loadAnimation.interpolator=BounceInterpolator()
           binding.cl.startAnimation(loadAnimation)

        }
        binding.qx.setOnClickListener {
            binding.cl.visibility=View.GONE
            val loadAnimation = AnimationUtils.loadAnimation(this, R.anim.`out`)
            loadAnimation.interpolator=BounceInterpolator()
            binding.cl.startAnimation(loadAnimation)
        }
        binding.R1.setOnCheckedChangeListener { radioGroup, i ->
            var para=""
            when(i){
                R.id.rb_typea->{
                    para="标准车位"
                }
                R.id.rb_typeb->{
                    para="VIP车位"
                }
            }
            lifecycleScope.launch {
                vm.H1REsult(para,"类型").collect{
                    car1Apter.submitData(it)
                }
            }

        }
        binding.R2.setOnCheckedChangeListener { radioGroup, i ->
            var para=""
            when(i){
                R.id.rb_statuskx->{
                    para="空闲"
                }
                R.id.rb_stauszy->{
                    para="已占用"
                }
                R.id.rb_stauswh->{
                    para="维修中"
                }
            }
            lifecycleScope.launch {
                vm.H1REsult(para,"状态").collect{
                    car1Apter.submitData(it)
                }
            }
        }

        binding.searchView.setOnQueryTextListener(object :androidx.appcompat.widget.SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                val toIntOrNull = query?.toIntOrNull()
                if (toIntOrNull!=null){
                  lifecycleScope.launch{
                      vm.H1REsult(toIntOrNull,"区域").collect{
                          car1Apter.submitData(it)
                      }
                  }
              }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
//                if (newText!=null){
//                    lifecycleScope.launch{
//                        vm.H1REsult(newText,"区域").collect{
//                            car1Apter.submitData(it)
//                        }
//                    }
//                }
                return false
            }

        })



        }




    override fun intiData() {

    }
}